TARGET=test
PWD=$(shell pwd)
CC=arm-none-eabi-gcc
OBJCOPY=arm-none-eabi-objcopy
CPUFLAGS=-mthumb -mcpu=cortex-m3
LDFLAGS = -T stm32_flash.ld -Wl,-cref,-u,Reset_Handler -Wl,-Map=$(TARGET).map -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80 -Wl,--start-group -lc -lm -Wl,--end-group
INCFLAGS=-I $(PWD)/cmsis -I $(PWD)/libraries/inc -I $(PWD)/user
CFLAGS=$(INCFLAGS) -D STM32F10X_HD -D USE_STDPERIPH_DRIVER -Wall -g -o
C_SRC=$(shell find . -name *.c)
C_OBJ=$(C_SRC:%.c=%.o)

START_SRC=$(shell find . -name startup_stm32f10x_s)
START_OBJ=$(START_SRC:%.s=%.o)

$(TARGET):$(START_OBJ) $(C_OBJ)
	$(CC) $^ $(CPUFLAGS) $(LDFLAGS) $(CFLAGS) $(TARGET).elf
$(START_OBJ):$(START_SRC)
	$(CC) -c $^ $(CPUFLAGS) $(CFLAGS) $@
$(C_OBJ):%.o:%.c
	$(CC) -c $^ $(CPUFLAGS) $(CFLAGS) $@ -D STM32F10X_HD

bin:
	$(OBJCOPY) $(TARGET).elf $(TARGET).bin
hex:
	$(OBJCOPY) $(TARGET).elf -Oihex $(TARGET).hex
clean:
	rm -f $(shell find . -name *.o) $(TARGET).*
